
//listeners are functions

function Event(observee){
	this._observee=observee;
	this._listeners=[];
	
}
Event.prototype={
		"constructor":"Event",
		"addListeners":function(listeners){
			this._listeners.push(listeners);
		},
		"notity":function(){
			for(var i=0,listenersLength=this._listeners.length;i<listenersLength;i++){
				this._listeners[i](this._observee);
			}
		}
}


function Model(items){
	this._items=items;
	this.itemAdded=new Event(this);
}
Model.prototype={
		"constructor":"Model",
		"getItems":function(){
			return this._items;
		},
		"addItem":function(item){
			this._items.push(item);
			this.itemAdded.notity();
		}
}



function View(model,components){
	this._model=model;
	this._components=components;
	var that=this;
	
	this.queryAllClicked=new Event(this); //按钮被触发
	
	this._components.qa_btn.click(function(){
		that.queryAllClicked.notity();
	});
	
	this._model.itemAdded.addListeners(function(){
		that.rebuild;
	});
	
}
View.prototype={
		"constructor":"View",
		"show":function(){
			this.rebuild;
		},
		"rebuild":function(){
			//get data from model and render to specified component
			var bookDiv=this._components.bookDiv;
			bookDiv.html("");
			var books=this._model.getItems();
			for(i in books){
				console.log(books);
				bookDiv.append(books[i].bookid+"&nbsp;"+books[i].bookname+"<br></br>");
			}
		}
}


function Controller(model,view){
	this._model=model;
	this._view=view;
	var that=this;
	
	this._view.queryAllClicked.addListeners(function(){
		that.queryAllItem(that);
	});
	
	
}
Controller.prototype={
		"constructor":"Controller",
		"queryAllItem":function(that){
			//get data from service and set it to model
			$.ajax({
				"url":"/springMVCBookms20170807/book/showAllBook",
				"type":"GET",
				"success":function(data){
					that._model._items = [];
					for(i in data){
						that._model.addItem(data[i]);
					}
				}
			});
		}
}
